package me.tuyou.cglib;

import me.tuyou.bean.MyInterface;
import me.tuyou.bean.MyInterfaceImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Description
 * @Author tuyou_cd
 * @Date 2020/6/1 19:04
 */
public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before");
        return methodProxy.invokeSuper(o, objects);
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(MyInterfaceImpl.class);
        enhancer.setCallback(new MyMethodInterceptor());

        MyInterface myInterface = (MyInterface) enhancer.create();
        myInterface.say("tuyou");
    }
}
